package com.sail.redis;

import lombok.extern.slf4j.Slf4j;
import org.redisson.spring.starter.RedissonAutoConfigurationV2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.net.InetAddress;
import java.net.UnknownHostException;

@EnableAsync
@EnableScheduling
@SpringBootApplication(exclude = {RedissonAutoConfigurationV2.class})
@Slf4j
public class App implements CommandLineRunner {

    @Value("${spring.profiles.active}")
    private String profilesActive;

    public static void main(String[] args) throws UnknownHostException {
        SpringApplication app = new SpringApplication(App.class);
        ConfigurableApplicationContext application = app.run(args);
        Environment env = application.getEnvironment();
        String tips =
                "----------------------------------------------------------\n" +
                        "Application %s is running! Access URLs:\n" +
                        "Local: http://localhost:%s\n" +
                        "External: 	http://%s:%s\n" +
                        "Doc: 	http://%s:%s/doc.html\n" +
                        "----------------------------------------------------------\n";

        String port = env.getProperty("server.port");
        log.info(String.format(tips,
                env.getProperty("spring.application.name"),
                port,
                InetAddress.getLocalHost().getHostAddress(),
                port,
                InetAddress.getLocalHost().getHostAddress(),
                port));
    }

    @Override
    public void run(String... args) {
        log.info("FINE. profile:{}", profilesActive);
    }
}
